from flask import Blueprint
from deco import Deco
from config import Config
from common import missionPanel, mConf

Configure = Blueprint("configure", __name__)

@Configure.route("/configure/<int:id>/get", methods = ["POST"])
@Deco
def getConfigure(p, id):
    if str(id) not in mConf["mission"]:
        return False, "id 不存在"
    c = mConf["mission"][str(id)]["config"]
    ret = {}
    for i in p["paraList"]:
        if i not in c:
            return False, "参数 %s 未定义" % i
        ret[i] = c[i]
    return True, "获取成功", ret

@Configure.route("/configure/<int:id>/set", methods = ["POST"])
@Deco
def setConfigure(p, id):
    if str(id) not in mConf["mission"]:
        return False, "id 不存在"
    c = mConf["mission"][str(id)]["config"]
    for i in p["paraDict"]:
        c[i] = p["paraDict"][i]
    mConf.save()
    missionPanel.reloadStore()
    return True, "设置成功"

@Configure.route("/configure/<int:id>/rewrite", methods = ["POST"])
@Deco
def rewriteConfigure(p, id):
    if str(id) not in mConf["mission"]:
        return False, "id 不存在"
    c = mConf["mission"][str(id)]["config"] = p["paraDict"]
    print(p)
    print(mConf["mission"])
    mConf.save()
    missionPanel.reloadStore()
    return True, "设置成功"

@Configure.route("/configure/<int:id>/clear", methods = ["POST"])
@Deco
def clearConfigure(p, id):
    if str(id) not in mConf["mission"]:
        return False, "id 不存在"
    mConf["mission"][str(id)]["config"] = {}
    mConf.save()
    missionPanel.reloadStore()
    return True, "清除成功"